var models = require('../db');
var express = require('express');
const jwt = require('jsonwebtoken');
const xlsx = require('node-xlsx');
var router = express.Router();
// var uuid = require('node-uuid');
var router = express.Router();
var mysql = require('mysql');

// 连接数据库
var conn = mysql.createConnection(models.mysql);

conn.connect();

// 登录接口
router.post("/commit", (req, res) => {
	const m = xlsx.parse('./document/插入宿舍信息.xlsx')
	errinfo = ''
	m.forEach(item => {
		for(let i = 1; i < item['data'].length; i++) {
			const sql = `insert into apartment.apartment_info(buildNo,dormNo,bedNo,dormType,capacity,sex,isLive) values(
			"${ item['data'][i][0]}",
			"${ item['data'][i][1]}",
			"${ item['data'][i][2]}",
			"${ item['data'][i][3]}",
			"${ item['data'][i][4]}",
			"${ item['data'][i][5]}",
			"${ item['data'][i][6]}")`
			conn.query(sql,[], function (err, result) {
				console.log("1"+global.errinfo);
				if (err!=null){
					errinfoi = errinfo+err.sqlMessage.match(/'(.*?)'/)[0];
					return errinfoi
				}
				
			})
			errinfo=errinfoi
console.log("1234"+errinfo);
		}
	})
	/* if (errinfo==''){
		
	return res.send({
		status: 201,
		msg: "部分数据插入失败",
		data:errinfo
		})
	}
	else {
		return res.send({
			status: 200,
			msg: "插入成功"
		})
	
	}
	 */
})

module.exports = router;
